import time, os

from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_personal_user_locator import PersonalUserLocator
from Case.python_ui.Test_data.user_data import User


class PerSonalUser(Basepage):

    def enter_pesonal_user_page(self):
        doc = '进入到personal_user页面'
        for i in range(3):
            try:
                self.wait_elevisble(PersonalUserLocator.pesonal_user_tab, doc)
                self.click_element(PersonalUserLocator.pesonal_user_tab, doc)
                self.wait_elevisble(PersonalUserLocator.organization_tab, doc)
                self.click_element(PersonalUserLocator.organization_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        self.wait_elevisble(PersonalUserLocator.page_total, doc)

    def query_user(self, user_name):
        doc = '查询用户'
        self.enter_pesonal_user_page()
        self.wait_elevisble(PersonalUserLocator.account_phone_query_input, doc)
        self.keyboard_event(PersonalUserLocator.account_phone_query_input, doc)
        time.sleep(2)
        self.send_keys(PersonalUserLocator.account_phone_query_input, user_name, doc)
        self.move_mouse_to_click_element(PersonalUserLocator.account_phone_query_button, doc)
        self.wait_elevisble(PersonalUserLocator.query_result_element, doc)
        eles = self.get_elements(PersonalUserLocator.query_result_elements, doc)
        return eles

    def query_nonexistent_user(self, user_name):
        doc = '查询不存在用户'
        self.enter_pesonal_user_page()
        self.wait_elevisble(PersonalUserLocator.account_phone_query_input, doc)
        self.keyboard_event(PersonalUserLocator.account_phone_query_input, doc)
        time.sleep(2)
        self.send_keys(PersonalUserLocator.account_phone_query_input, user_name, doc)
        self.move_mouse_to_click_element(PersonalUserLocator.account_phone_query_button, doc)
        self.wait_eleinvisble(PersonalUserLocator.page_total, doc)
        time.sleep(2)
        eles = self.get_elements(PersonalUserLocator.query_result_elements, doc)
        return eles

    def download_all_user_information(self, file_path):
        doc = '下载用户数据'
        self.enter_pesonal_user_page()
        time.sleep(10)
        self.click_element(PersonalUserLocator.download_button, doc)
        self.save_screen(doc+'-点击下载后')
        time.sleep(20)
        # 获取下载目录中最新的文件
        list_of_files = os.listdir(file_path)
        latest_file = list_of_files[-1]
        print(latest_file)
        return latest_file

    def page_filtering_function_retrieval(self, user_name):
        doc = '筛选查询'
        self.enter_pesonal_user_page()
        self.query_user(user_name)
        self.wait_elevisble(PersonalUserLocator.status_filter, doc)
        time.sleep(2)
        self.move_mouse_to_click_element(PersonalUserLocator.status_filter, doc)
        self.wait_elevisble(PersonalUserLocator.filter_normal, doc)
        time.sleep(2)
        self.move_mouse_to_click_element(PersonalUserLocator.filter_normal, doc)
        self.click_element(PersonalUserLocator.filter_ok, doc)
        self.wait_elevisble(PersonalUserLocator.status, doc)
        ele = self.get_elements(PersonalUserLocator.status, doc)
        status = ele[0].text
        return status

    def list_columns(self):
        doc = "用户列表字段校验"

        self.enter_pesonal_user_page()
        self.wait_eleinvisble(PersonalUserLocator.loading_wait, doc)

        time.sleep(5)
        list_columns = self.get_elements(PersonalUserLocator.list_columns, doc)
        assert list_columns[0].text == "Account Email"
        assert self.get_elements(PersonalUserLocator.column_status, doc)[0].text == "Status"
        assert self.get_elements(PersonalUserLocator.column_status, doc)[1].text == "Dunning Status"
        assert list_columns[3].text == "User Name"
        assert list_columns[4].text == "Phone Number"
        assert list_columns[5].text == "User Group"
        assert list_columns[6].text == "Organization"
        assert list_columns[7].text.startswith("Creation Time")
        assert "Actions" in list_columns[8].text
        # 判断 status 选项
        self.get_elements(PersonalUserLocator.status_checkbox, doc)[0].click()
        time.sleep(1)
        status_options = self.get_elements(PersonalUserLocator.status_checkbox_options, doc)

        assert status_options[0].text == "Normal"
        assert status_options[1].text == "Locked"
        assert status_options[2].text == "Inactive"
        # 判断 dunning status 选项
        self.refresh_page()
        time.sleep(5)
        self.get_elements(PersonalUserLocator.status_checkbox, doc)[1].click()
        time.sleep(1)
        status_options = self.get_elements(PersonalUserLocator.status_checkbox_options, doc)

        assert status_options[0].text == "Normal"
        assert status_options[1].text == "Grace Period"
        assert status_options[2].text == "Dunning 1"
        assert status_options[3].text == "Dunning 2"
        assert status_options[4].text == "Dunning 3"

        assert self.get_element(PersonalUserLocator.reset_pwd_button, doc).text == "Reset Password"

        self.move_mouse_to_element(PersonalUserLocator.user_options_ellipsis)
        time.sleep(2)
        assert self.get_element(PersonalUserLocator.lock_button, doc).text == "Lock"
        assert self.get_element(PersonalUserLocator.unlock_button, doc).text == "Unlock"

    def reset_pwd(self):
        doc = "重置用户密码"
        time.sleep(5)
        self.click_element(PersonalUserLocator.reset_pwd_button, doc)

        self.wait_elevisble(PersonalUserLocator.accept_ack, doc)
        prompt_lines = self.get_elements(PersonalUserLocator.reset_prompt_lines, doc)
        assert prompt_lines[0].text == "User must agree with you to help reset password."
        assert prompt_lines[1].text == "New password will be sent to user via selected channel(s)."

        self.click_element(PersonalUserLocator.accept_ack, doc)
        time.sleep(2)
        self.click_element(PersonalUserLocator.confirm_button, doc)
        self.wait_elevisble(PersonalUserLocator.success_pup, doc)
        assert self.get_element(PersonalUserLocator.success_pup, doc).text == "Reset password successfully."

    def lock_user(self, cmdopt):
        self.query_user(User.personal_user_email[cmdopt])

        doc = "锁定用户"
        self.wait_eleinvisble(PersonalUserLocator.loading_wait, doc)
        self.move_mouse_to_element(PersonalUserLocator.user_options_ellipsis)
        time.sleep(2)
        self.click_element(PersonalUserLocator.lock_button_area, doc)

        self.wait_elevisble(PersonalUserLocator.accept_ack, doc)
        prompt_lines = self.get_elements(PersonalUserLocator.lock_prompt_lines, doc)
        assert prompt_lines[0].text == "The user was confirmed to be unsecure on system."
        assert prompt_lines[1].text == "After locking account, user can not access system from next login try."

        self.click_element(PersonalUserLocator.accept_ack, doc)
        time.sleep(2)
        self.click_element(PersonalUserLocator.confirm_button, doc)
        self.wait_elevisble(PersonalUserLocator.success_pup, doc)
        assert self.get_element(PersonalUserLocator.success_pup, doc).text == "Locked successfully."

        self.refresh_page()


    def unlock_user(self, cmdopt):

        time.sleep(5)
        self.query_user(User.personal_user_email[cmdopt])

        doc = "解锁用户"

        self.wait_eleinvisble(PersonalUserLocator.loading_wait, doc)
        self.move_mouse_to_element(PersonalUserLocator.user_options_ellipsis)
        time.sleep(2)
        self.click_element(PersonalUserLocator.unlock_button_area, doc)

        self.wait_elevisble(PersonalUserLocator.accept_ack, doc)
        prompt_lines = self.get_elements(PersonalUserLocator.lock_prompt_lines, doc)
        assert prompt_lines[0].text == "The user was confirmed to be secure on system."
        assert prompt_lines[1].text == "After unlocking account, user can access system from next login try."

        self.click_element(PersonalUserLocator.accept_ack, doc)
        time.sleep(2)
        self.click_element(PersonalUserLocator.confirm_button, doc)
        self.wait_elevisble(PersonalUserLocator.success_pup, doc)
        assert self.get_element(PersonalUserLocator.success_pup, doc).text == "Unlocked successfully."


